package com.huaxindata.common.base;

import android.app.Activity;
import android.app.Dialog;
import android.app.DialogFragment;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

import com.huaxindata.common.R;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * @author songxf
 * @date 2018/5/28
 * @Desc BaseDialogFragment基类
 * Copyright HUAXIN DATA. All rights reserved.
 */
public abstract class HxBaseDialogFragment extends DialogFragment {
    private Activity mActivity;
    private Unbinder mUnbinder;
    private Dialog   mDialog;
    private boolean isFillWidth   = false;
    /**
     * 默认高度WRAP_CONTENT
     */
    private int     heightDefault = ViewGroup.LayoutParams.WRAP_CONTENT;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity = getActivity();
    }


    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        View rootView = View.inflate(mActivity, setRootViewId(), null);
        mUnbinder = ButterKnife.bind(this, rootView);
        mDialog = new Dialog(mActivity, R.style.dialog_style);
        mDialog.setContentView(rootView);
        mDialog.setCanceledOnTouchOutside(true);
        Window alertWindow = mDialog.getWindow();
        alertWindow.setGravity(showPosition());
        initData();
        setWidthOnHeight(getWidthContent(), heightDefault);
        return mDialog;
    }

    /**
     * 设置宽高
     *
     * @param width
     * @param height
     */
    public void setWidthOnHeight(int width, int height) {
        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        mDialog.getWindow().setLayout(width, height);
    }

    /**
     * 默认设置宽度*0.85
     *
     * @return
     */
    private int getWidthContent() {
        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        return (int) (dm.widthPixels * 0.85);
    }


    public abstract int setRootViewId();

    public abstract void initData();

    public abstract int showPosition();

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mUnbinder.unbind();
    }

}
